package al.xc.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单ID生成
 */
public class OrderIDUtils {
    public static Object index = (Integer)0;

    public static String newOrderID(int valleft, int valright) throws Exception {
        if (valleft < 0 || valleft > 999) {
            throw new Exception("valleft invalid, need in [0,999]");
        }
        if (valright < 0 || valright > 999) {
            throw new Exception("valright invalid, need in [0,999]");
        }
        String nowTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        return nowTime + indexString() + String.format("%03d", valleft) + String.format("%03d", valright) + String.format("%06d", RandomUtils.next(100000,999999));
    }

    private synchronized static String indexString() {
        int val = (Integer)index;
        val = (++val) % 1000000;
        if (val < 132542) {
            val = 132542;
        }
        index = val;
        return String.format("%06d", val);
    }
}
